#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int longestPalindrome(vector<string>& words) {
        int cnt[26][26] = { 0 };
        for (auto& str : words)
        {
            cnt[str[0] - 'a'][str[1] - 'a']++;
        }
        int res = 0;
        bool flag = false;
        for (int i = 0; i < 26; i++)
        {
            for (int j = i; j < 26; j++)
            {
                if (i == j)
                {
                    res += cnt[i][j] / 2;
                    if (cnt[i][j] % 2 != 0)
                        flag = true;
                }
                else
                    res += min(cnt[i][j], cnt[j][i]);
            }
        }
        return (res * 4 + flag * 2);
    }
};